package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import DAO.*;
import model.*;

public class QueStudy_NoticeController extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			processRequest(request, response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		try {
			processRequest(request, response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		String uri = request.getRequestURI();
	
		String command = uri.substring(uri.lastIndexOf("/") + 1);

		ActionForward forward = new ActionForward();
		Action action = null;

	
		 if (command != null && command.trim().equals("New.Notice")){
			 HttpSession session=request.getSession();
				
				
				
				int studyId = request.getParameter("studyId") != null
						&& request.getParameter("studyId") != "" ? Integer
						.parseInt(request.getParameter("studyId")) : 0;
				int userIdCheck = session.getAttribute("userid") != null
						&& session.getAttribute("userid") != "" ? Integer
						.parseInt((String)session.getAttribute("userid")) : 0;
				if (userIdCheck < 1) {
					session.invalidate();
					Util.ShowError(response, "로그인해주세요");
				}
				if (studyId < 1) {
					Util.ShowError(response, "잘못 된 접근입니다");
				}
				
				
			//StudyDao studyDao = new StudyDao();
			//DepositDao depositDao = new DepositDao();
			//FineDao fineDao = new FineDao();
			//LanguageDao languageDao = new LanguageDao();
			
			//request.setAttribute("categories", studyDao.getAllCategory());
			//request.setAttribute("deposit",depositDao.getAllDeposit());
			//request.setAttribute("fine",fineDao.getAllFine());
			//request.setAttribute("language",languageDao.getAllLanguage());
				request.setAttribute("studyId", studyId);	
			forward =  new ActionForward();
			forward.setPath("/study_notice_makeNotice.jsp");
			
		}
		 else if (command != null && command.trim().equals("Modify.Notice")){
			 HttpSession session=request.getSession();
			Notice notice = new Notice();
			NoticeDao noticeDao = new NoticeDao();
			
				int studyId = request.getParameter("studyId") != null
						&& request.getParameter("studyId") != "" ? Integer
						.parseInt(request.getParameter("studyId")) : 0;
				int userIdCheck = session.getAttribute("userid") != null
						&& session.getAttribute("userid") != "" ? Integer
						.parseInt((String)session.getAttribute("userid")) : 0;
				if (userIdCheck < 1) {
					session.invalidate();
					Util.ShowError(response, "로그인해주세요");
				}
				if (studyId < 1) {
					Util.ShowError(response, "잘못 된 접근입니다");
				}
				
				int noticeId = request.getParameter("noticeId") != null
						&& request.getParameter("noticeId") != "" ? Integer
								.parseInt(request.getParameter("noticeId")) : 0;
				if(noticeId>0)
				{
				notice=noticeDao.getNoticeInfo(noticeId);
				request.setAttribute("notice", notice);
				}

				request.setAttribute("studyId", studyId);	
			forward =  new ActionForward();
			forward.setPath("/study_notice_modifyNotice.jsp");
			
		}
		else if (command != null && command.trim().equals("NewAction.Notice")){
			action = new NoticeNewAction();
			try {

				forward = action.execute(request, response);

			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}
		else if (command != null && command.trim().equals("List.Notice")) {

			action = new NoticeListAction();
			try {

				forward = action.execute(request, response);

			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		else if (command != null && command.trim().equals("Info.Notice")){
			action = new NoticeInfoAction();
			try {

				forward = action.execute(request, response);

			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}
		else if (command != null && command.trim().equals("ModifyAction.Notice")){
			action = new NoticeModifyAction();
			try {

				forward = action.execute(request, response);

			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}

		if (forward != null) {
			if (forward.isRedirect()) {
				response.sendRedirect(forward.getPath());

			} else {
				RequestDispatcher dispatcher = request
						.getRequestDispatcher(forward.getPath());
				System.out.println(dispatcher.toString());

				dispatcher.forward(request, response);
			}
		}
	}
}
